package com.hks360.car_treasure.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.view.View;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;


import com.hks360.car_treasure.application.MyApplication;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;


/**
 * Created by rwq on 2016/4/14.
 */
/*
* 功能1：显示字符串或者资源字符串吐司
* 功能2：将Double类型的数字转换成人民币
* 功能3：将long整数转换成Byte表示
* 功能4：打开一个Activity：openActivityForResult，openActivity
* 功能5：获取Intent
* 功能6：判断是否为空:数组，集合，Map，TextView，EditText
* 功能7：创建一个PopupWindow，显示一个PopupWindow
* 功能8：判断一个App是否存在，安装一个Apk文件，获取一个Apk的版本
* **/
public class CommonUtil {
    //判断一个App是否存在
    public static boolean isExistApp(Context context, String packageName) {
        boolean flag = false;
        PackageManager pm = context.getPackageManager();
        try {
            PackageInfo pi = pm.getPackageInfo(packageName, 0);
            if (pi != null) {
                flag = true;
            }
        } catch (PackageManager.NameNotFoundException e) {
            flag = false;
        }
        return flag;
    }
    //安装一个Apk文件
    public static void installApk(Context context, String filePath) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.parse("file://" + filePath), "application/vnd.android.package-archive");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
    //获取一个Apk的版本
    public static final String[] getApkVersion(Context context, String packageName) {
        try {
            String[] versionArr = new String[2];
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(packageName, 0);
            String versionCode = String.valueOf(info.versionCode);
            String versionName = info.versionName;
            versionArr[0] = versionCode;
            versionArr[1] = versionName;
            return versionArr;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    //打开一个Activity
    public static void openActivity(Context context, Class<?> activityClass) {
        Intent intent = new Intent();
        intent.setClass(context, activityClass);
        context.startActivity(intent);
    }
    //打开一个Activity
    public static void openActivityForResult(Activity context,Class<?> activityClass,int requestCode) {
        Intent intent = new Intent();
        intent.setClass(context,activityClass);
        context.startActivityForResult(intent, requestCode);
    }
    //获取Intent
    public static Intent getIntent(Context context, Class<?> activityClass) {
        Intent intent = new Intent();
        intent.setClass(context, activityClass);
        return intent;
    }
    //判断一个数组是否为空，默认是
    public static <E> boolean isEmpty(E e) {
        Class c = e.getClass();
        //数组是否为空的判断，对象数组
        if(c.getName().charAt(0) == '['){
            if (c.getName().length()>2){
                return isEmptyArray((Object[])e);
            }else {
                switch (c.getName()){
                    case "[I":
                        if (e == null || ((int[])e).length == 0) {
                            return true;
                        }
                        return false;
                    case "[J":
                        if (e == null || ((long[])e).length == 0) {
                            return true;
                        }
                        return false;
                    case "[D":
                        if (e == null || ((double[])e).length == 0) {
                            return true;
                        }
                        return false;
                    case "[Z":
                        if (e == null || ((boolean[])e).length == 0) {
                            return true;
                        }
                        return false;
                    case "[C":
                        if (e == null || ((char[])e).length == 0) {
                            return true;
                        }
                        return false;
                    case "[F":
                        if (e == null || ((float[])e).length == 0) {
                            return true;
                        }
                        return false;

                }
            }
        }
        switch (c.getName()) {
            case "android.widget.EditText":
                if (e == null || ((EditText) e).getText().toString().trim().length() == 0) {
                    return true;
                }
                return false;
            case "android.widget.TextView":
                if (e == null || ((TextView)e).getText().toString().trim().length() == 0) {
                    return true;
                }
                return false;
            case "java.util.HashMap":
            case "java.util.Map":
                if (e == null || ((Map)e).isEmpty()) {
                    return true;
                }
                return false;
            case "java.lang.String":
                if (e == null || ((String)e).length() == 0) {
                    return true;
                }
                return false;
            case "java.util.List":
            case "java.util.ArrayList":
                if (e == null || ((List)e).isEmpty()) {
                    return true;
                }
                return false;
        }
        return true;
    }
    private static boolean isEmptyArray(Object[] obj) {
        if (obj == null || obj.length == 0) {
            return true;
        }
        return false;
    }
    //创建一个PopupWindow
    public static PopupWindow createPopupWindow(View view) {
        PopupWindow popupWindow = new PopupWindow(view);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        popupWindow.setFocusable(true);
        return popupWindow;
    }
    //显示一个PopupWindow
    public static void showPopup(PopupWindow popupWindow, View view) {
        if (popupWindow.isShowing()) {
            popupWindow.dismiss();
        } else {
            popupWindow.showAsDropDown(view);
        }
    }

    //显示个字符串或者资源字符串吐司
    public static <E> void showToast(E text) {
        Class c = text.getClass();
        LogUtil.e(c.getName());
        switch (c.getName()){
            case "java.lang.Integer":
                Toast.makeText(MyApplication.getInstance(), (Integer) text, Toast.LENGTH_SHORT).show();
                break;
            case "java.lang.String":
                Toast.makeText(MyApplication.getInstance(), (CharSequence) text, Toast.LENGTH_SHORT).show();
                break;
        }
    }
    //将Double类型的数字转换成人民币
    public static String digitToRmb(double n) {
        String fraction[] = { "角", "分" };
        String digit[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
        String unit[][] = { { "元", "万", "亿" }, { "", "拾", "佰", "仟" } };
        String head = n < 0 ? "负" : "";
        n = Math.abs(n);
        String s = "";
        for (int i = 0; i < fraction.length; i++) {
            s += (digit[(int) (Math.floor(n * 10 * Math.pow(10, i)) % 10)] + fraction[i]).replaceAll("(零.)+", "");
        }
        if (s.length() < 1) {
            s = "整";
        }
        int integerPart = (int) Math.floor(n);

        for (int i = 0; i < unit[0].length && integerPart > 0; i++) {
            String p = "";
            for (int j = 0; j < unit[1].length && n > 0; j++) {
                p = digit[integerPart % 10] + unit[1][j] + p;
                integerPart = integerPart / 10;
            }
            s = p.replaceAll("(零.)*零$", "").replaceAll("^$", "零") + unit[0][i] + s;
        }
        return head	+ s.replaceAll("(零.)*零元", "元").replaceFirst("(零.)+", "").replaceAll("(零.)+", "零")
                .replaceAll("^整$", "零元整");
    }
    //将long整数转换成Byte表示
    public static String digitToByte(long fileSize) {
        DecimalFormat format = new DecimalFormat("0.0");
        if (fileSize < 1024) {
            return String.valueOf(fileSize) + "B";
        }
        double sizeKB = fileSize * 1.0 / 1024;
        if (sizeKB < 1024) {
            return format.format(sizeKB) + "KB";
        }
        double sizeMB = sizeKB / 1024;
        if (sizeMB < 1024) {
            return format.format(sizeMB) + "MB";
        }
        return fileSize == 0 ? "0" : String.valueOf(fileSize);
    }

}
